import java.util.ArrayList;
import java.util.List;

//leetcode 46 全排列
public class Permute {
    private boolean[] check;
    private List<List<Integer>> list;
    private List<Integer> path;
    public List<List<Integer>> permute(int[] nums) {
        check = new boolean[nums.length];
        list = new ArrayList<>();
        path = new ArrayList<>();
        dfs(nums);
        return list;
    }

    private void dfs(int[] nums) {
        if (path.size() == nums.length) {
            list.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!check[i]) {
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                //回溯
                check[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }
}
